/**
 * related to temple.ui
 * 
 * @Author : wzgl5533
 * @Timestamp : 2017-07-29 寺庙列表页
 */
var dojs = require("dojs");
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var do_Global = sm("do_Global");
var do_Network = sm("do_Network");
var do_ListView = ui("do_ListView_temple");
var do_listData = mm("do_ListData");
var do_DataCache = sm("do_DataCache");
var do_Network = sm("do_Network");
// 声明变量
var root = ui("root");
var searchEdt = ui("do_TextField_search");
var templeCallback;
// 获取初始化配置数据
var currentOption = do_Page.getData();
// 访问变量
var totalPage = 0;
var params;
var templeData;
var currentPage = 0;
var currentProvince = "";
var currentStatus = "EXECUTED";
var currentSearchStr = "";
var currentOrderBy = "count desc";
var isRefresh = true;
// 引入标题栏，128px
root.add("topBar", "source://view/common/topbar.ui", 0, 0);
// 引入地址选择
root.add("addressSelect", "source://modules/addressselect/src/addressSelect.ui", 0, 0);
// 引入空布局
root.add("empty", "source://modules/empty/empty.ui", 0, 0);
var empty = ui("empty");
empty.visible = false;
// 引入排序布局
root.add("sort", "source://modules/popmenu/popmenu.ui", 0, 0);

initView();
// 筛选条件
var provinceID = null;
var locationLy = ui("do_ALayout_quanguo");
var sortLy = ui("do_ALayout_sort");
var location = ui("do_Label_location");
var sort = ui("do_Label_sort");
dojs.style.css(locationLy,"dynamicButton");
dojs.style.css(sortLy,"dynamicButton");
dojs.page.onTouch(locationLy,function(){
	// 弹出地址选择框
	showAddress();
});
// 订阅获取最终地址消息
do_Page.on("finalAddress",function(data){
	
	location.text = data.province;
	currentPage = 0;
	isRefresh = true;
	currentProvince = data.provinceID; 
	queryRecord();
})

dojs.page.onTouch(sortLy,function(){
	
	do_Page.fire("menu_show");
});
// 绑定数据
do_ListView.bindItems(do_listData);
// 订阅查询寺庙列表信息完成消息
do_Page.on("templeInfoCompleted", function(data) {
	
	if (dojs.core.isNullData(data.rows)) {
		if (isRefresh) {
			do_ListView.visible = false;
			empty.visible = true;
		}else {
			dojs.core.toast("没有更多数据");
		}
		return;
	}
	empty.visible = false;
	do_ListView.visible = true;
	
	totalPage = data.totalCount;
	// 赋值
	templeData = getTempleData(data.rows);
	if (isRefresh) {
		do_listData.removeAll();
	}
	do_listData.addData(templeData);
	do_ListView.refreshItems();
	if (isRefresh) {
		do_DataCache.saveData(do_Global.getMemory("data_label").temple_info,
				data);
	}
});
// 选中的item，跳转详情页
do_Page.on("selectItem", function(item) {
	var entrance = require("mymodule/entrance");
	entrance.openTempleDetail.openTempleDetailPage(do_listData.getOne(item));
});
// 列表监听事件
do_ListView.on("touch", function(index) {
	do_Page.fire("selectItem", index);
});
//获取选择的排序条件
do_Page.on("menu_select",function(data){
	
	if (data.status == "CREATETIME") {
		currentStatus = "EXECUTED";
		currentOrderBy = "createTime desc";
	}else {
		currentStatus = data.status;
		if (data.status == "EXECUTED") {
			currentOrderBy = "count desc";
		}else {
			currentOrderBy = "count asc";
		}
	}
	sort.text = data.text;
	isRefresh = true;
	currentPage = 0;
	
	queryRecord();
});
// 下拉列表，刷新数据
do_ListView.on("pull", function(data) {
	// 其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2) {
		refreshAllData();
	}
});
// 上拉列表，翻页数据
do_ListView.on("push", function(data) {
	// 其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){		
		loadMore();
	}
});
//搜索寺庙
searchEdt.on("textChanged",function(){
	
	var content = searchEdt.text.trim();
	isRefresh = true;
	currentPage = 0;
	currentSearchStr = content;
	
	queryRecord();
});

// -------------------------------------------------------------------------
function initView() {
	//地址初始化
	do_Page.fire("initAddressSelect",{type:1});
	
	if (dojs.core.isNullData(currentOption)) {
		dojs.core.toast("初始化数据为空！");
	} else {
		// 初始化界面

		// 发消息,初始化标题栏
		do_Page.fire("initTopBarMsg", {
			text : dojs.core.isNullData(currentOption.title.name) ? "" : "请"
					+ currentOption.title.name + "香-找寺庙",
			isBack : currentOption.allowClose
		});
	}
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接");
		return;
	}
	if (dojs.core.isNullData(currentOption.onCallback)) {
		return;
	}
	templeCallback = require(currentOption.onCallback);
	// 查询
	queryRecord();

}

// ------------------------------------------------------------------------
/**
 * 寺庙列表数据
 * 
 * @data 数组
 */
function getTempleData(data) {
	var test = require("source://view/temple/test");
	return test.paraseTempleInfo(data,currentPage);
}

// 查询---------------------------------------------------------------------
function queryRecord() {
	
	params = {
			currentPage:currentPage,
			labels: currentOption.title.id,
			province:currentProvince,
			status:currentStatus,
			searchStr:currentSearchStr,
			orderBy:currentOrderBy
	};
	templeCallback.invoke(params, do_ListView);
}

// 刷新数据------------------------------------------------------------------
function refreshAllData() {
	
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接");
		return;
	}
	currentPage = 0;
	isRefresh = true;
	queryRecord();
}

// 加载更多-----------------------------------------------------------------
function loadMore() {
	
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接");
		return;
	}
	// 检测是否有更多数据，主要是比较当前的页数是否达到总页数
    var expectNextPage = currentPage + 1;
    if (expectNextPage + 1 <= totalPage) {
    	currentPage ++;
    	isRefresh = false;
    	queryRecord();
	}
	
}

// 显示地址选择----------------------------------------------------------------
function showAddress() {
	
	do_Page.fire("showView");
	
}
// 当前页面下，订阅android系统返回键的事件
do_Page.on("back", function() {
	do_App.closePage();
});